package com.example.base.tools;

import android.app.Activity;
import android.app.Application;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;

import com.example.base.tools.config.AppProfile;
import com.example.base.tools.config.CorePreferences;

import java.util.ArrayList;

public class CoreApplication extends Application {
    private static final String TAG = CoreApplication.class.getSimpleName();
    public static Handler mHandler;
    private static CoreApplication BaseApplication;
    private ArrayList<Activity> activities = new ArrayList<Activity>();

    public static synchronized CoreApplication getInstance() {

        return BaseApplication;
    }
    private void init() {
        //        // 异常处理，不需要处理时注释掉这两句即可！
//        CrashHandler crashHandler = CrashHandler.getInstance();
//        // 注册crashHandler
//        if (!AppProfile.isApkDebugable(getApplicationContext())) {
//            crashHandler.init(this);
//        }

        mHandler = new Handler();

        CorePreferences.getInstance(this).initConfig(this);
//         初始化应用信息
        AppProfile.getInstance(this).init();

    }

    public static Handler getHandler() {
        return mHandler;
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
    }
    public void addActivity(Activity activity) {
        activities.add(activity);
    }
    public void finishAll() {
        for (Activity activity : activities) {
            activity.finish();
        }
    }

    public int getClientVersion() {
        int versionCode;
        try {
            PackageManager pm = getPackageManager();
            PackageInfo pi = pm.getPackageInfo(getPackageName(), 0);
            versionCode = pi.versionCode;// 获取在AndroidManifest.xml中配置的版本号
        } catch (PackageManager.NameNotFoundException e) {
            versionCode = 0;
        }
        return versionCode;
    }

    public String getClientVersionName() {
        String versionName;
        try {
            PackageManager pm = getPackageManager();
            PackageInfo pi = pm.getPackageInfo(getPackageName(), 0);
            versionName = pi.versionName;// 获取在AndroidManifest.xml中配置的版本号
        } catch (PackageManager.NameNotFoundException e) {
            versionName = "";
        }
        return versionName;
    }

}
